import { reactive, ref, toRefs } from "vue"

type paginationParams = { pageNum: number; pageSize: number }

export default function useTable<
    T extends { list: any[]; total: number },
    P extends paginationParams
>(fn: (params: P) => RES<T>, searchParams?: Object) {
    const paramas = reactive<paginationParams>({
        pageNum: 1,
        pageSize: 10,
    })

    let list = ref<T['list']>([]);

    const total = ref(0)
    const fetch = () => {
        fn({...(paramas as P),...searchParams}).then((res) => {
            if (res.code === 200) {
                list.value = res.data.list
                total.value = res.data.total
            }
        })
    }
    fetch()
    // 分页器左右
    const pageChangeHandle = () => {
        fetch();
    }
    // 分页器数字
    const pageSizeChangeHandle = () => {
        fetch();
    }
    // 点击搜索或者重置
    const ListFn = () => {
        fetch();
    }
    return {
        list, total, ...toRefs(paramas), pageChangeHandle, pageSizeChangeHandle, ListFn
    }
}